<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*" import="java.io.*" import="java.text.*" %>
    
<!--details content coded by Karla Barber-->
<div id="main">

<%!
	//declaring public variables
	private String driver;
	private String connection;
	private String user;
	private String password;
	NumberFormat nf = NumberFormat.getCurrencyInstance();
	NumberFormat nfI = NumberFormat.getIntegerInstance();
%>		
						
<% 
	//to link back to the login.jsp page, uncomment the following:
	//String sessionFlag = ((String) session.getAttribute("login"));
	driver = application.getInitParameter("oracle_driver");
	connection = application.getInitParameter("oracle_db");
	user = application.getInitParameter("oracledb_user");
	password = application.getInitParameter("oracledb_password");
%>

			<h2>Booking Details</h2>
			<%
					//connect to database using web.xml stored parameters
					Class.forName(driver);
					
					String custId = (String)session.getAttribute("customerid");
					String bookId = (String)request.getParameter("id");
					System.out.println(bookId);
					//String custId = "119";
					//String bookId = "84";
					
					//retreive the customer information
					Connection connectCustomer = DriverManager.getConnection(connection, user, password);
					java.sql.Statement stmntCustomer = connectCustomer.createStatement();
					ResultSet resGetCust = stmntCustomer.executeQuery("SELECT c.*, b.* from ictoosd.customers c, ictoosd.bookings b " +
																	  "where c.customerid = " + custId + " " +
																	  "and b.bookingid = " + bookId + " " +
																	  "and c.customerid = b.customerid");
					
					if (resGetCust.next())
					{
						//print the beginning of the table and row
						out.println("<table width = '500'><tr>");
						out.println("<td width = '30%'>" + resGetCust.getString("custlastname") + ", " + resGetCust.getString("custfirstname") + "</td>");
						out.println("<td width = '30%'></td>");
						out.println("<td width = '40%'>Booking Date:  " + resGetCust.getDate("bookingdate") + "</td>");
						out.println("</tr>");
						out.println("<tr>");
						out.println("<td>" + resGetCust.getString("custaddress") + "</td>");
						out.println("<td></td>");
						out.println("<td>Booking Number:  " + resGetCust.getString("bookingno") + "</td>");
						out.println("</tr>");
						out.println("<tr><td>" + resGetCust.getString("custcity") + ", " + resGetCust.getString("custprov") + " " + resGetCust.getString("custpostal") + "</td>");
						out.println("<td></td>");
						out.println("<td>Traveler Count:  " + nfI.format(resGetCust.getFloat("travelercount")) + "</td>");
						out.println("</tr>");	
						out.println("</table>");
						out.println("<br/><br/>");
						
						if (resGetCust.getInt("packageid") > 0)
						{
							java.sql.Statement stmntPackage = connectCustomer.createStatement();
							ResultSet resGetPkg = stmntCustomer.executeQuery("select p.*, pr.prodname, s.supname " +
																			"from ictoosd.PACKAGES p, ictoosd.packages_products_suppliers pps, ictoosd.products_suppliers ps, ictoosd.products pr, ictoosd.suppliers s " +
																			"where p.packageid = " + resGetCust.getInt("packageid") + " " +
																			"and p.packageid = pps.packageid " +
																			"and pps.productsupplierid = ps.productsupplierid " +
																			"and ps.productid = pr.productid " +
																			"and ps.supplierid = s.supplierid");
							if (resGetPkg.next())
							{
								//output headers for the package detail table
								out.println("<table width = '500'><tr>");
								out.println("<th width = '30%'>Package Name</th>");
								out.println("<th>Package Start Date</th>");
								out.println("<th>Package End Date</th>");
								out.println("<th></th>");
								out.println("<th>Package Price</th>");
								out.println("</tr>");								

								//output package information
								out.println("<tr>");
								out.println("<td width = '30%'>" + resGetPkg.getString("pkgname") + "</td>");
								out.println("<td>" + resGetPkg.getDate("pkgstartdate") + "</td>");
								out.println("<td>" + resGetPkg.getDate("pkgenddate") + "</td>");
								out.println("<td></td>");
								out.println("<td align='right'>" + nf.format(resGetPkg.getInt("pkgbaseprice")) + "</td>");
								out.println("</tr>");
								out.println("</table>");
								
								out.println("<table width = '500'>");
								out.println("<tr>");
								out.println("<td width = '20%'></td>");
								out.println("<td>" + resGetPkg.getString("prodname") + " - " + resGetPkg.getString("supname") + "</td>");
								out.println("</tr>");

								while (resGetPkg.next())
								{
									out.println("<tr>");
									out.println("<td></td>");
									out.println("<td>" + resGetPkg.getString("prodname") + " - " + resGetPkg.getString("supname") + "</td>");
									out.println("</tr>");
								}
								out.println("</table>");
							}
						}
						else
						{
							java.sql.Statement stmntDetails = connectCustomer.createStatement();
							ResultSet resGetDtl = stmntDetails.executeQuery("select * from ictoosd.bookingdetails " +
																			"where bookingid = " + bookId);
							
							java.sql.Statement stmntTotal = connectCustomer.createStatement();
							ResultSet resGetTot = stmntTotal.executeQuery("select sum(baseprice) from ictoosd.bookingdetails " +
																		  "where bookingid = " + bookId);
							
							if (resGetDtl.next())
							{
								//output headers for the package detail table
								out.println("<table width = '500'><tr>");
								out.println("<th>Itinerary No</th>");
								out.println("<th>Trip Start</th>");
								out.println("<th>Trip End</th>");
								out.println("<th>Description</th>");
								out.println("<th>Price</th>");
								out.println("</tr>");
								
								//output package information
								out.println("<tr>");
								out.println("<td>" + nfI.format(resGetDtl.getFloat("itineraryno")) + "</td>");
								out.println("<td>" + resGetDtl.getDate("tripstart") + "</td>");
								out.println("<td>" + resGetDtl.getDate("tripend") + "</td>");
								out.println("<td>" + resGetDtl.getString("Description") + "</td>");
								out.println("<td align='right'>" + nf.format(resGetDtl.getDouble("baseprice")) + "</td>");
								out.println("</tr>");
							}

							while (resGetDtl.next())
							{
								//output package information
								out.println("<tr>");
								out.println("<td>" + nfI.format(resGetDtl.getFloat("itineraryno")) + "</td>");
								out.println("<td>" + resGetDtl.getDate("tripstart") + "</td>");
								out.println("<td>" + resGetDtl.getDate("tripend") + "</td>");
								out.println("<td>" + resGetDtl.getString("Description") + "</td>");
								out.println("<td align='right'>" + nf.format(resGetDtl.getDouble("baseprice")) + "</td>");
								out.println("</tr>");
							}
							
							if (resGetTot.next())
							{
								//output package information
								out.println("<tr>");
								out.println("<td></td>");
								out.println("<td></td>");
								out.println("<td></td>");
								out.println("<td>Total Cost</td>");
								out.println("<td align='right'>" + nf.format(resGetTot.getLong(1)) + "</td>");
								out.println("</tr>");
								out.println("</table>");
							}
						}
					}
					//close connection
					connectCustomer.close();
			 %>
			 
			 <%  //Dagny added the logic to make the button appear or not appear depending
			 	 //on what page the user is on, regular details or printdetails
				String currentPage = request.getServletPath();
			 	if (currentPage.equals("/details.jsp"))
			 	{
			 		//display the button to go to a more print friendly form
			 %>
			 
			 <!-- this section changed by Karla Barber -->
			 		<form>
						<input name="formbutton" class="button" type="button" onClick="location.href='detailsprint.jsp?id=<%= (String)request.getParameter("id")%>'" value='click for printer friendly format'>
					</form>
			 <!-- end changes by Karla Barber -->
			<%
				}
			%>			 
			 
			<br/>
			<br/>
		</div>
<!-- main ends -->	

